package cur.nature.component.nacos.controller;

import cur.nature.component.nacos.config.MicroClient;
import cur.nature.component.nacos.property.RestConfig;
import cur.nature.framework.core.log.LOG;
import io.netty.handler.timeout.ReadTimeoutException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.reactive.function.client.WebClientResponseException;

@Tag(name = "Nacos 服务接口")
@RestController
@RequestMapping("component/nacos/v1/api")
@ConditionalOnProperty( value = {"cur.component.nacos.controller.enable"})
public class Nacos_ServerController {

    @Autowired
    private RestConfig config;

    @Operation(summary = "本地测试接口")
    @GetMapping("/test")
    public String test() throws Exception {
        LOG.info("test : " );
//        Thread.sleep(10000);
        return "1";
    }

    @Operation(summary = "调用Nacos服务")
    @GetMapping("/call")
    public String call() {
        LOG.info("call : " );
        try {
            String result = null;
//            String result = MicroClient.get().getForObject("cur-admin-service"+"/component/nacos/v1/api/test1", String.class);
            LOG.info("result: "+result);
            return result;
        } catch (ResourceAccessException e) {
            // 读取数据超时, 熔断, 可以定制化处理, 也可以在外面
            LOG.error(e.getMessage());
            throw e;
        } catch (Exception e) {
            // 请求异常
            LOG.error(e.getMessage());
            throw e;
        }
    }
    @Operation(summary = "测试webclient")
    @GetMapping("/webclient")
    public String testWebclient() {
        LOG.info("webclient : " );
        try {
            final String result = MicroClient.get().uri("http://"+"cur-admin-service"+"/component/nacos/v1/api/test")
                    .retrieve().bodyToMono(String.class).block();
            LOG.info("result: "+result);
            return result;
        } catch (ReadTimeoutException e) {
            // 读取数据超时, 熔断, 可以定制化处理, 也可以在外面
            LOG.error(e.getMessage());
            throw e;
        } catch (Exception e) {
            if (e.getCause() != null && e.getCause() instanceof ReadTimeoutException) {
                LOG.error("read timeout error :"+e.getCause().getMessage());
            }
//            if (e instanceof WebClientRequestException) {
//                WebClientRequestException responseException = (WebClientRequestException) e;
//                LOG.error("error :"+responseException.getMessage());
//            }
            if (e instanceof WebClientResponseException) {
                WebClientResponseException responseException = (WebClientResponseException) e;
                LOG.error("error :"+responseException.getStatusCode() +", " + responseException.getStatusText());
            }

            // 请求异常
            LOG.error(e.getMessage());
            throw e;
        }
    }

    @Operation(summary = "测试动态属性配置")
    @GetMapping("/testconfig")
    public String testconfig() throws Exception {
        LOG.info("name : " + config.getConnectTimeout() );
        return config.getConnectTimeout()+"";
    }


//    public String callNacosService(String serviceName, String endpoint) {
//
//        return MicroserviceClient.get().getForObject(serviceName+endpoint, String.class);
////        return MicroserviceClient.getForObject("http://" + serviceName + "/", endpoint, String.class);
////        ServiceInstance instance = loadBalancer.choose(serviceName);
////        String requestUrl = String.format("http://%s:%s%s", instance.getHost(), instance.getPort(), endpoint);
////        LOG.info("callNacosService requestUrl: "+requestUrl);
////        return restTemplate.getForObject(requestUrl, String.class);
//    }


}

